package com.jeejio.train.service.impl;

import com.jeejio.train.dao.CourseMapper;
import com.jeejio.train.entity.Course;
import com.jeejio.train.service.CourseService;
import com.jeejio.train.util.IConstant;
import com.jeejio.train.util.UserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper = null;

    public Course findById(Integer id) {
        return courseMapper.selectByPrimaryKey(id);
    }

    public boolean create(Course course) {
        return courseMapper.insert(course) > 0;
    }

    @Override
    public List<Course> findByDeliveryDate(Date deliveryDate, byte draft) {
        return courseMapper.selectByDeliveryDate(deliveryDate, draft);
    }

    @Override
    public boolean modify(Course course) {
        return courseMapper.updateByPrimaryKey(course) > 0;
    }

    @Override
    public boolean deleteByLecturer(Course course) {
        return courseMapper.deleteLecturerCourseByID(course.getId()
                , course.getLecturer().getId()) > 0;
    }

    @Override
    public List<Date> findHasCourseDates(Date between, Date and) throws UserException {
        if (and.before(between)) {
            throw new UserException(IConstant.E_DATE_END_BEFORE_START, "截止日期不能早于起始日期");
        }
        return courseMapper.findHasCourseDates(between, and);
    }

    @Override
    public List<Course> findHistory() {
        return courseMapper.selectBeforeDate(new Date());
    }
}
